package org.project.product.android.pfe.fanta.domain;

import org.project.product.android.pfe.fanta.R;
import org.project.product.android.pfe.fanta.adapter.DataManagerFacade;
import org.project.product.android.pfe_fanta.Home;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.Toast;

public class DeleteUserDialog extends Dialog {

	User user;
	private final Context context;

	public DeleteUserDialog(Context context, User user) {
		super(context);
		this.user = user;
		this.context = context;
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.setContentView(R.layout.delete_user_dialog);
		findViewById(R.id.bt_cancel).setOnClickListener(
		        new View.OnClickListener() {

			        @Override
			        public void onClick(View v) {
				        dismiss();
			        }
		        });
		findViewById(R.id.bt_delete).setOnClickListener(
		        new View.OnClickListener() {

			        @Override
			        public void onClick(View v) {
				        deleteProduct();
			        }
		        });

	}

	public void deleteProduct() {
		EditText quantityField = (EditText) findViewById(R.id.edit_quantity_number);
		User user = DataManagerFacade.getInstance(getOwnerActivity())
		        .retrieveUserByCodePin(quantityField.getText().toString());
		if (user != null) {
			DataManagerFacade.getInstance(context).deleteUser(user);
			backHome();
		} else {
			Toast.makeText(context, "Erreur", Toast.LENGTH_LONG).show();
		}

	}

	private void backHome() {
		Intent intent = new Intent(context, Home.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		context.startActivity(intent);
	}
}
